package cn.itsource.ymcc.cofig;

import cn.itsource.ymcc.domain.Permission;
import cn.itsource.ymcc.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.stereotype.Component;

import java.util.List;

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfiger extends WebSecurityConfigurerAdapter {

    //提供用户信息，这里没有从数据库查询用户信息，在内存中模拟
//    @Bean
//    public UserDetailsService userDetailsService(){
//        InMemoryUserDetailsManager inMemoryUserDetailsManager =
//                new InMemoryUserDetailsManager();
//        inMemoryUserDetailsManager.createUser(User.withUsername("zs").password("123").authorities("admin").build());
//        return inMemoryUserDetailsManager;
//    }

    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    //密码编码器：不加密
    @Bean
    public PasswordEncoder passwordEncoder(){

        return new BCryptPasswordEncoder();
    }

    @Autowired
    private IPermissionService permissionService;
    //授权规则配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
            // web 授权，通过查询数据库表，设置所有的资源 resource 和 Sn 的关系
//        List<Permission> permissions = permissionService.selectList(null);
//
//        for (Permission permission : permissions) {
//            http.authorizeRequests().antMatchers(permission.getResource()).hasAuthority(permission.getSn());
//        }

        http.authorizeRequests()                                //授权配置
                .antMatchers("/login","/login.html","/login/common","/login/refresh").permitAll()  //登录路径放行
                .antMatchers("/login", "/oauth/authorize").permitAll()
//                .antMatchers("/login/list").hasAuthority("login:list")
//                .antMatchers("/permission/list").hasAuthority("permission:list")
                .anyRequest().authenticated()                   //其他路径都要认证之后才能访问
                .and().formLogin()                              //允许表单登录
//                .successForwardUrl("/loginSuccess")             // 设置登陆成功页
                //.loginPage("/login.html")                       //登录页面跳转地址
                //.loginProcessingUrl("/login")                   //登录处理地址(必须)

                .and().logout().permitAll()                    //登出路径放行 /logout
                .and().csrf().disable();                        //关闭跨域伪造检查
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder();
        String encode = bCrypt.encode("1");
        String encode1 = bCrypt.encode("1");
        String encode2 = bCrypt.encode("1");

        /*
        $2a$10$UT3d..sU.A3h6QamkxZ9yOWiUjViaSaqzrOxRti3uln89fAXZCnLK
        $2a$10$KUMi1sptdbBJgEMnC1zNdOl0tBUTOlzF0GQv9ia.NdcWp5TQTn3Ri
        $2a$10$pMj0nk7NLAxP.LOpoWUHa.pPpCFKgaF3Z9MZFNsN3TicnLxTcfT/6
         */
        System.out.println(encode);
        System.out.println(encode1);
        System.out.println(encode2);

        System.out.println(bCrypt.matches("1", "$2a$10$IG4pMJpRd0XPXlkP/eTahOIuuv8dVJrwNJYQ7ow53veiBTytV8iPi"));
//        System.out.println(bCrypt.matches("1", "$2a$10$UT3d..sU.A3h6QamkxZ9yOWiUjViaSaqzrOxRti3uln89fAXZCnLK"));
//        System.out.println(bCrypt.matches("1", "$2a$10$pMj0nk7NLAxP.LOpoWUHa.pPpCFKgaF3Z9MZFNsN3TicnLxTcfT/6"));

    }

}
